/*
 * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR C¬ONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sermant.core.utils;

import java.util.Collection;

/**
 * Collection utility class
 *
 * @author lilai
 * @since 2023-07-17
 */
public class CollectionUtils {
    private CollectionUtils() {
    }

    /**
     * Determines whether the collection is null or has no elements
     *
     * @param collection collection
     * @return result
     */
    public static boolean isEmpty(Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }

    /**
     * Get the first element of the collection
     *
     * @param collection collection
     * @param <T>  type
     * @param orElse when the collection is null or empty, return this
     * @return first element of the collection, or orElse if the collection is null or empty
     */
    public static <T> T getFirst(Collection<T> collection, T orElse) {
        return isEmpty(collection) ? orElse : collection.iterator().next();
    }
}
